<?php
header('Content-Type: text/html; charset=utf-8');
					mb_internal_encoding('UTF-8');
	class NhaCungCap extends CI_Controller
	{
		function admin_index()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->LayNCC1(); //nhà cung cấp đang hợp tác
				
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcap_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function ngunghoptac()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->LayNCC2(); //nhà cung cấp đang hợp tác
				
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcapngung_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function dangkymoi() // ds đăng ký mới
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->LayDKMoi();
				
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcapdkmoi_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function xemdangkymoi() // cap nhat tinh trang duyet = 1
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$result = $this->nhacungcap_model->CapNhatTinhTrangDuyet($mancc);
				
					if($result)
					{
						?>
	        	        <script>
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/motrangchitiet/<?php echo $mancc ?>";
						</script>
    	            	<?php
					}
					else
					{
						?>
    		            <script>
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/motrangchitiet/<?php echo $mancc ?>";
						</script>
	                	<?php
					}
				}
			}
			else
				redirect('login/admin_index');
		}
		function nccduyet() // ds đăng ký mới
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->LayNCCDuyet();
				
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcapduyet_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		/*function xuatfile()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$this->load->library('cezpdf');
				$this->load->helper('pdf');
				prep_pdf();	
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$data = $this->nhacungcap_model->xuatfile($mancc);
					foreach($data as $row)
					{
						$mancc = $row->MaNCC;
						$tenncc = $row->Ten;
						$diachi = $row->DiaChi;		
						$dienthoai = $row->DienThoai;
						$email = $row->Email;
						$fax = $row->Fax;			
						$masothue = $row->MaSoThue;
						$tinhtrang = $row->TinhTrangTaiKhoan;
						$tenks = $row->tenks;
						$maks = $row->maks;
						$diachiks = 'Số ' . $row->sn . ', đường ' . $row->d . ', quận ' . $row->q . ', tỉnh/thành phố ' . $row->tp . ', nước ' . $row->qg;
						$dienthoaiks = $row->dt;
						$emailks = $row->em;
						$tieuchuan = $row->tc;
						$quydinh = $row->qd;
						$thongtin = $row->tt;
						$vat = $row->vat;
						$phidichvu = $row->p;
						$hoahong = $row->hh;
						$str = "THÔNG TIN NHÀ CUNG CẤP\r\n\r\nTên: $tenncc\r\n\r\nĐịa chỉ: $diachi\r\n\r\nĐiện thoại: $dienthoai\r\n\r\nEmail: $email\r\n\r\nFax: $fax\r\n\r\nMã số thuế: $masothue\r\n\r\n\r\n\r\nTHÔNG TIN KHÁCH SẠN\r\n\r\nTên khách sạn: $tenks\r\n\r\nĐịa chỉ: $diachiks\r\n\r\nĐiện thoại: $dienthoaiks\r\n\r\nTiêu chuẩn: $tieuchuan\r\n\r\nThông tin: $thongtin\r\n\r\nQuy định: $quydinh\r\n\r\nVAT: $vat%\r\n\r\nPhí dịch vụ: $phidichvu.000VNĐ\r\n\r\nHoa hồng: $hoahong%";
						//$str = iconv('UTF-8', 'VISCII', $str);
						//$str = iconv("UTF-8", "SJIS", $str);
						$str = iconv('UTF-8', 'CP1258', $str);
						
					}
					
				}
				
					$this->cezpdf->ezText($str, 10);
					$this->cezpdf->ezStream();
					
				
				
			}
			else
				redirect('login/admin_index');
		}*/
		
		function xuatfile()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$result = $this->nhacungcap_model->xuatfile($mancc);
				
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	        	        <script>
							alert("Xuất file thành công!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/motrangchitiet/<?php echo $mancc ?>";
						</script>
    	            	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
    		            <script>
							alert("Xuất file thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/motrangchitiet/<?php echo $mancc ?>";
						</script>
	                	<?php
					}
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function motrangchitiet() // mở lại trang chi tiết để quyết đinh hợp tác hay không
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$data['nhacungcap'] = $this->nhacungcap_model->LayChiTietDuyetNhaCungCap($mancc);
					//$data['nhacungcap'] = $this->nhacungcap_model->CapNhatTinhTrangDuyet($mancc);
						
										
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/chitietduyet_nhacungcap_view', $data);
				}
			}
			else
				redirect('login/admin_index');
		}
	
		
		function khonghoptac() // duyet khong hop tac
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$result = $this->nhacungcap_model->KhongHopTac($mancc);
				
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	        	        <script>
							alert("Thực hiện thành công!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/nccduyet";
						</script>
    	            	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
    		            <script>
							alert("Thực hiện thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/nccduyet";
						</script>
	                	<?php
					}
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function hoptac()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$result = $this->nhacungcap_model->HopTac($mancc);
				
					if($result)
					{						
						// Gửi email thông bao username va pass
						$emailfrom = 'i12.onlinebooking@gmail.com';
						$emailpass = 'onlinebooking';
						$sender = 'i12 Online Booking';
					
						$emailto = $this->nhacungcap_model->layemailncc($mancc);
						$username =  $this->nhacungcap_model->SetUsername($mancc);
						$password =  $this->nhacungcap_model->SetPass($mancc);
						$subject = "Thông tin hợp tác";
						$message = "Xin chào ông/bà\r\nCông ty chúng tôi rất vui vì đã hợp tác với ông/bà. Đây là tên đăng nhập và mật khẩu của ông/bà trên trang web của chúng tôi.\r\nUsername: $username\r\n Password: $password";
					
						$config = array(
										'protocol' => 'smtp',
										'smtp_host' => 'ssl://smtp.googlemail.com',
										'smtp_port' => 465,
										'smtp_user' => $emailfrom,
										'smtp_pass' => $emailpass,
										'wordwrap' => FALSE
										);
									
						$this->load->library('email', $config);
						$this->email->set_newline("\r\n");
					
						$this->email->from($emailfrom, $sender);
						$this->email->to($emailto);
						$this->email->subject($subject);
						$this->email->message($message);
				
						if($this->email->send())
						{
							echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
							?>
							<script>
								alert("Thực hiện thành công. Thư xác nhận thông báo username và password đã được gửi đi.");
								window.location = "<?php echo base_url(); ?>index.php/nhacungcap/nccduyet";
							</script>
							<?php					
						}
						else
						{
							$this->load->model('nhacungcap_model');
							$result = $this->nhacungcap_model->HopTacTB($mancc);
							echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
							?>
							<script>
								alert("Thực hiện thất bại! Vui lòng thử lại");
								window.location = "<?php echo base_url(); ?>index.php/nhacungcap/nccduyet";
							</script>
							<?php					
						}
					
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
    		            <script>
							alert("Thực hiện thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/nccduyet";
						</script>
	                	<?php
					}
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function timncchh()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = $this->uri->segment(3);
				
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->TimNCCHH($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcap_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function timnccdkm()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = $this->uri->segment(3);
				
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->TimNCCDKM($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcapdkmoi_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function timnccd()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = $this->uri->segment(3);
				
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->TimNCCD($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcapduyet_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function timnccn()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = $this->uri->segment(3);
				
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->TimNCCN($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcapngung_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function laynhacungcaphh_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('nhacungcap_model');
				$data['tenncc'] = $this->nhacungcap_model->LayTenNCC_ajax($key);
				$data['tenkhachsan'] = $this->nhacungcap_model->LayTenKhachSan_ajax($key);
				
				$this->load->view('admin/ajax_danhsachncc', $data);
			}
			else
				redirect('login/admin_index');	
		}
		
		function laynhacungcapdkmoi_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('nhacungcap_model');
				$data['tenncc'] = $this->nhacungcap_model->LayTenNCCdkmoi_ajax($key);
				$data['tenkhachsan'] = $this->nhacungcap_model->LayTenKhachSandkmoi_ajax($key);
				
				$this->load->view('admin/ajax_danhsachncc', $data);
			}
			else
				redirect('login/admin_index');	
		}
		
		function laynhacungcapduyet_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('nhacungcap_model');
				$data['tenncc'] = $this->nhacungcap_model->LayTenNCCduyet_ajax($key);
				$data['tenkhachsan'] = $this->nhacungcap_model->LayTenKhachSanduyet_ajax($key);
				
				$this->load->view('admin/ajax_danhsachncc', $data);
			}
			else
				redirect('login/admin_index');	
		}
		
		function laynhacungcapngung_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('nhacungcap_model');
				$data['tenncc'] = $this->nhacungcap_model->LayTenNCCngung_ajax($key);
				$data['tenkhachsan'] = $this->nhacungcap_model->LayTenKhachSanngung_ajax($key);
				
				$this->load->view('admin/ajax_danhsachncc', $data);
			}
			else
				redirect('login/admin_index');	
		}
		
		function motrangcapnhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$data['nhacungcap'] = $this->nhacungcap_model->LayNhaCungCap($mancc);
						
										
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/capnhat_nhacungcap_view', $data);
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function capnhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$mancc = $this->input->post('mancc');
				$data = array(
								'Ten' => $this->input->post('tenncc'),
								'DiaChi' => $this->input->post('diachi'),
								'DienThoai' => $this->input->post('dienthoai'),
								'Email' => $this->input->post('email'),
								'Fax' => $this->input->post('fax'),
								'MaSothue' => $this->input->post('masothue'),
							);
				$this->load->model('nhacungcap_model');
				$result = $this->nhacungcap_model->CapNhat($data, $mancc);
				
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				if($result < 0)
				{
					?>
                    <script>
						alert('Thay đổi thông tin thất bại!');
						window.location = "<?php echo base_url(); ?>index.php/nhacungcap/motrangcapnhat/<?php echo $mancc; ?>";						
					</script>
                    <?php
					return;
				}
				
				?>
                <script>
					alert('Thông tin đã được thay đổi!');
					window.location = "<?php echo base_url(); ?>index.php/nhacungcap/motrangcapnhat/<?php echo $mancc; ?>";						
				</script>
                <?php				
			}
			else
				redirect('login/admin_index');
		}
		
		function huynhacungcap()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$result = $this->nhacungcap_model->CapNhatTinhTrang($mancc);
				
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	        	        <script>
							alert("Hủy nhà cung cấp thành công!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/admin_index";
						</script>
    	            	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
    		            <script>
							alert("Hủy nhà cung cấp thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/admin_index";
						</script>
	                	<?php
					}
				}
			}
			else
				redirect('login/admin_index');
		}
	
		function duyetlai()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$result = $this->nhacungcap_model->DuyetLai($mancc);
				
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	        	        <script>
							alert("Thực hiện thành công!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/ngunghoptac";
						</script>
    	            	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
    		            <script>
							alert("Thực hiện thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/nhacungcap/ngunghoptac";
						</script>
	                	<?php
					}
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function motrangchitietduyet() // hop
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$data['nhacungcap'] = $this->nhacungcap_model->LayChiTietDuyetNhaCungCap($mancc);
						
										
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/chitietduyet2_nhacungcap_view', $data);
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function chitietncc()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$data['nhacungcap'] = $this->nhacungcap_model->LayChiTietNhaCungCap($mancc);
						
										
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/chitietncc_nhacungcap_view', $data);
				}
			}
			else
				redirect('login/admin_index');
		}
		
		function chitietnccngung()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$mancc = $this->uri->segment(3);
				if($mancc)
				{
					$this->load->model('nhacungcap_model');
					$data['nhacungcap'] = $this->nhacungcap_model->LayChiTietNhaCungCap($mancc);
						
										
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/chitietncc2_nhacungcap_view', $data);
				}
			}
			else
				redirect('login/admin_index');
		}

		function tim()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = $this->uri->segment(3);
				
				$this->load->model('nhacungcap_model');
				$data['nhacungcap'] = $this->nhacungcap_model->Tim($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/nhacungcap_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		
//CONTROLLER CUA TRANG KHACH HANG.//////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
//CONTROLLER CUA TRANG KHACH HANG.//////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
//CONTROLLER CUA TRANG KHACH HANG.//////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

function index() {
        $login = $this->session->userdata('islogin_ncc');
        if ($login) {
            $data['current_username'] = $this->session->userdata('username_ncc');
			$mancc = $this->session->userdata('mancc');
			$this->load->model('nhacungcap_model');
			$data['ncc'] = $this->nhacungcap_model->GetARecord($mancc);
			$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/ThongtinNCC_view', $data);
        } else {
        	$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/nhacungcap_view',$data);
        }
    }

    function dangky() {
    	$this->load->model('khachsan_model');
        $data['country'] = $this->khachsan_model->layQuocGia();
        $this->load->view('home/Formdangki_ncc_view.php',$data);
    }

    function Them() {
    	$this->db->trans_begin();
    	$tinhtrangduyet = 0;
        $nhacungcap = array(
            'Ten' => $this->input->post('tenncc'),
            'DiaChi' => $this->input->post('dcncc'),
            'DienThoai' => $this->input->post('dtncc'),
            'Email' => $this->input->post('emailncc'),
            'Fax' => $this->input->post('faxncc'),
            'MaSoThue' => $this->input->post('mstncc'),
            'TinhTrangDuyet' => $tinhtrangduyet
        );

        $this->load->model('nhacungcap_model');
        $result = $this->nhacungcap_model->Them($nhacungcap);
        $mancc = $this->nhacungcap_model->LayNCCMoiNhat();
        if ($result) {
            $khachsan = array(
                'Ten' => $this->input->post('tenks'),
                'SoNha' => $this->input->post('soks'),
                'Duong' => $this->input->post('duongks'),
                'Quan' => $this->input->post('quanks'),
                'Tinh_ThanhPho' => $this->input->post('tpks'),
                'DienThoai' => $this->input->post('dtks'),
                'Email' => $this->input->post('emailks'),
                'ThongTin' => $this->input->post('infoks'),
                'QuyDinh' => $this->input->post('ruleks'),
                'TieuChuan' => $this->input->post('tieuchuanks'),
                'NhaCungCap' => $mancc,
            );

            $this->load->model('khachsan_model');
            $result = $this->khachsan_model->Them($khachsan);

            if ($result) {
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
                ?>
                <script>
                    alert("Đăng kí Thành công! Chúng tôi sẽ liên hệ sớm với quý công ty.");
                    window.location = "<?php echo site_url('nhacungcap') ?>";
                </script>
                <?php
            } else {
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
                ?>
                <script>
                    alert("Đăng kí thất bại, vui lòng thực hiện lại!");
                    window.location = "<?php echo site_url('nhacungcap/dangky') ?>";
                </script>
                <?php
            }
        } else {
			echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
            ?>
            <script>
                alert("Đăng kí thất bại, vui lòng thực hiện lại!");
                window.location = "<?php echo site_url('nhacungcap/dangky') ?>";
            </script>
            <?php
        }
		
		if ($this->db->trans_status() === FALSE)
		{
		    $this->db->trans_rollback();
		}
		else
		{
		    $this->db->trans_commit();
		}
    }

    function dangnhap() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->load->model('nhacungcap_model');
        $result = $this->nhacungcap_model->CheckLogin($username, $password);
        if ($result == 1) {
            $mancc = $this->nhacungcap_model->LayMaNCC($username);
            $session_data = array('mancc' => $mancc, 'username_ncc' => $username, 'islogin_ncc' => 1);
            $this->session->set_userdata($session_data);
            redirect('nhacungcap/index');
        }
        else
            echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
            ?>
            <script>
                alert("Thông tin đăng nhập không hợp lệ, vui lòng đăng nhập lại!");
                window.location = "<?php echo site_url('nhacungcap/index') ?>";
        </script>
        <?php
    }

    function Logout() {
        $this->session->unset_userdata('mancc');
        $this->session->unset_userdata('username_ncc');
        $this->session->unset_userdata('islogin_ncc');
        $this->session->sess_destroy();
        redirect('nhacungcap/index', 'refresh');
    }

    function thongtinncc() {
        $login = $this->session->userdata('islogin_ncc');
        if ($login) {
            $data['current_username'] = $this->session->userdata('username_ncc');
			$mancc = $this->session->userdata('mancc');
			$this->load->model('nhacungcap_model');
			$data['ncc'] = $this->nhacungcap_model->GetARecord($mancc);
			$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/ThongtinNCC_view', $data);
        } else {
        	$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/nhacungcap_view',$data);
        }
    }

    function thongtinks() {
        $login = $this->session->userdata('islogin_ncc');
        if ($login) {
            $data['current_username'] = $this->session->userdata('username_ncc');
			$mancc = $this->session->userdata('mancc');
			$this->load->model('khachsan_model');
			$maks =  $this->khachsan_model->laymaKS($mancc);
			$data['khachsan'] = $this->khachsan_model->LayThongTinKhachSan($maks);
			$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/ThongtinKS_view', $data);
        } else {
        	$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/nhacungcap_view',$data);
        }
    }
	
	function quenmatkhau(){
			$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/nhacungcap_quenpass_view',$data);
	}
	
	function yeucaumatkhau(){
		$tenNCC = $this->input->post('tenncc');
		$username_ncc = $this->input->post('username_ncc');
		$dtNCC = $this->input->post('dtncc');
		$this->load->model('nhacungcap_model');
		$mancc = $this->nhacungcap_model->LayMaNCC($username_ncc);
		if($mancc){
			$this->load->model('nhacungcap_model');
			$ncc = $this->nhacungcap_model->LayChiTietNhaCungCap($mancc);
			foreach ($ncc as $row)
			{
				$ten = $row->Ten;
				$dt = $row->DienThoai;
				$email = $row->Email;
				
				if ($ten == $tenNCC && $dt == $dtNCC){
					$newpassword = $this->nhacungcap_model->SetPass($mancc);
					
					// Gửi email password
						$emailfrom = 'i12.onlinebooking@gmail.com';
						$emailpass = 'onlinebooking';
						$sender = 'i12 Online Booking';
					
						$emailto = $email;
						$subject = "[i12]New password";
						$message = "i12 gởi đến bạn mật khẩu mới:".$newpassword."\r\n 
						Vui lòng thử đăng nhập và liên lạc liền với chúng tôi nếu có sự cô. \r\n
						Xin chân thành cảm ơn, \r\n
						i12 Online Booking";
					
						$config = array(
										'protocol' => 'smtp',
										'smtp_host' => 'ssl://smtp.googlemail.com',
										'smtp_port' => 465,
										'smtp_user' => $emailfrom,
										'smtp_pass' => $emailpass,
										'wordwrap' => FALSE
										);
									
						$this->load->library('email', $config);
						$this->email->set_newline("\r\n");
					
						$this->email->from($emailfrom, $sender);
						$this->email->to($emailto);
						$this->email->subject($subject);
						$this->email->message($message);
					
						if($this->email->send())
						{
							echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
							?>
	    		    	    <script>
								alert("Thư cấp mật khẩu mới đã được gửi đi. Kiểm tra email để có mật khẩu mới. ");
								window.location = "<?php echo site_url('nhacungcap') ?>";
							</script>
				            <?php					
						}
						else
						{
							echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
							?>
		    		        <script>
								alert("Không gửi được thư cấp mật khẩu mới.");
								window.location = "<?php echo site_url('nhacungcap/quenmatkhau') ?>";
							</script>
				            <?php					
						}
					
				}else{
					?>
	                <script>
						alert('Thông tin Tên Nhà Cung cấp hoặc Số Điện Thoại bị sai!');
						window.location = "<?php echo site_url('nhacungcap/quenmatkhau') ?>";					
					</script>
	                <?php
				}
			}//het foreach
			
		}else{
			?>
            <script>
				alert('Không có thông tin về tên đăng nhập này!');
				window.location = "<?php echo site_url('nhacungcap/quenmatkhau') ?>";					
			</script>
            <?php
		}
	}
	
	function thaydoimatkhau(){
		$login = $this->session->userdata('islogin_ncc');
        if ($login) {
            $data['current_username'] = $this->session->userdata('username_ncc');
			$mancc = $this->session->userdata('mancc');
			$this->load->model('nhacungcap_model');
			$data['ncc'] = $this->nhacungcap_model->GetARecord($mancc);
			$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/NCCSuamatkhau_view', $data);
        } else {
        	$this->load->model('khachsan_model');
        	$data['country'] = $this->khachsan_model->layQuocGia();
            $this->load->view('home/nhacungcap_view',$data);
        }
	}
	
	function updateNCC(){
		$login = $this->session->userdata('islogin_ncc');
		$mancc = $this->session->userdata('mancc');
        if ($login) {
				$data = array(
								'Ten' => $this->input->post('tenncc'),
								'DiaChi' => $this->input->post('dcncc'),
								'DienThoai' => $this->input->post('dtncc'),
								'Email' => $this->input->post('emailncc'),
								'Fax' => $this->input->post('faxncc'),
								'MaSothue' => $this->input->post('mstncc'),
							);
				$this->load->model('nhacungcap_model');
				$result = $this->nhacungcap_model->CapNhat($data, $mancc);
				
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				if($result)
				{
					?>
                    <script>
						alert('Thông tin đã được Cập nhật!');
						window.location = "<?php echo site_url('nhacungcap/thongtinncc') ?>";					
					</script>
                    <?php
				}
				
				?>
                <script>
					alert('Thay đổi thông tin không thành công! Vui lòng thực hiện lại!');
					window.location = "<?php echo site_url('nhacungcap/thongtinncc') ?>";					
				</script>
                <?php	
        }else{
        	redirect('home/ThongtinNCC_view');
        }
	}
	
	function updateks(){
		$login = $this->session->userdata('islogin_ncc');
        if ($login) {
				$maks = $this->input->post('maks');
				$data = array(
								'Ten' => $this->input->post('tenks'),
								'SoNha' => $this->input->post('soks'),
								'Duong' => $this->input->post('duongks'),
								'Quan' => $this->input->post('quanks'),
								'Tinh_ThanhPho' => $this->input->post('tpks'),
								'QuocGia' => $this->input->post('quocgiaks'),
								'Email' => $this->input->post('emailks'),
								'DienThoai' => $this->input->post('dtks'),
								'ThongTin' => $this->input->post('infoks'),
								'QuyDinh' => $this->input->post('ruleks'),
								'TieuChuan' => $this->input->post('tieuchuanks'),
							);
				$this->load->model('khachsan_model');
				$result = $this->khachsan_model->CapNhat($data, $maks);
				
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				if($result)
				{
					?>
                    <script>
						alert('Thông tin đã được Cập nhật!');
						window.location = "<?php echo site_url('nhacungcap/thongtinks') ?>";					
					</script>
                    <?php
				}
				
				?>
                <script>
					alert('Thay đổi thông tin không thành công! Vui lòng thực hiện lại!');
					window.location = "<?php echo site_url('nhacungcap/thongtinks') ?>";					
				</script>
                <?php	
        }else{
        	redirect('home/ThongtinNCC_view');
        }
	}
	
	function updatepass(){
		$login = $this->session->userdata('islogin_ncc');
		$mancc = $this->session->userdata('mancc');
		$username = $this->session->userdata('username_ncc');
		$this->load->model('nhacungcap_model');
		$ncc = $this->nhacungcap_model->GetARecord($mancc);
		
        if ($login) {
        	$old_pass = $this->input->post('old_pass');
			$new_pass = $this->input->post('new_pass');
			$new_pass_retype = $this->input->post('new_pass_retype');
			
        	$result = $this->nhacungcap_model->CheckLogin($username, $old_pass);
			
	        if ($result == 1) {//neu dung voi password cu thi thuc hien cap nhat password moi
	            $new_pass = md5($new_pass);
	            $data = array(
									'Password' => $new_pass,
								);
				$this->load->model('nhacungcap_model');
				$result = $this->nhacungcap_model->CapNhat($data, $mancc);
				
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				if($result)//neu cap nhat duoc thi thanh cong
				{
					?>
                    <script>
						alert('Thông tin đã được Cập nhật!');
						window.location = "<?php echo site_url('nhacungcap/thongtinncc') ?>";					
					</script>
                    <?php
				}else{
				
				?>
                <script>
					alert('Thay đổi thông tin không thành công! Vui lòng thực hiện lại!');
					window.location = "<?php echo site_url('nhacungcap/thaydoimatkhau') ?>";					
				</script>
                <?php
				}
                
	        }
	        else
	            echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
	            ?>
	            <script>
	                alert("Mật khẩu cũ không trùng khớp. Vui lòng thực hiện lại!");
	                window.location = "<?php echo site_url('nhacungcap/thaydoimatkhau') ?>";
	        </script>
	        <?php
				
		}else{//cho phan khong dang login
        	redirect('home/ThongtinNCC_view');
        }
	}

	function goiyquocgia_ajax()
	{
		$key = $_GET['term'];		
		$this->load->model('khachsan_model');
		$data['tenquocgia'] = $this->khachsan_model->LayTenQuocGia_ajax($key);											
				
		$this->load->view('home/ajax_danhsachquocgia', $data);
	}
	
	function goiythanhpho_ajax()
	{
		$key = $_GET['term'];		
		$this->load->model('khachsan_model');
		$data['tenthanhpho'] = $this->khachsan_model->LayTenThanhPho_ajax($key);											
				
		$this->load->view('home/ajax_danhsachthanhpho', $data);
	}
	
	function goiyquan_ajax()
	{
		$key = $_GET['term'];		
		$this->load->model('khachsan_model');
		$data['tenquan'] = $this->khachsan_model->LayTenQuan_ajax($key);											
				
		$this->load->view('home/ajax_danhsachquan', $data);
	}


}?>
